
//
// Copyright 2009 Arne Halvorsen (arne.halvorsen@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Project page: http://code.google.com/p/mf4jexamples/

//
// 091116a- aha42 - Created.
// 091116b- aha42 - Java doc fix.
// 091118 - aha42 - Took in use new FlickrSession constructor accepting 
//                  CommandLineParser.
// 091121 - aha42 - Added project page link (google code) to header comment.
//

package org.aha.mf4j.examples;

import java.io.IOException;

import org.aha.mf4j.FlickrSession;
import org.aha.mf4j.FlickrUrls;
import org.aha.mf4j.Request;
import org.aha.mf4j.response.FlickrObject;
import org.aha.mf4j.util.CommandLineParser;

/**
 * <p>
 *   Sample code for accessing Flickr photo data, generate source URL 
 *   specifications and HTML code that can be used to include photos in no web
 *   pages outside of the 'Flickerverse'.
 * </p>
 * @author Arne Halvorsen (aha42)
 */
public final class GetPhotoInfo 
{
  private GetPhotoInfo(){} // Pure main method class, never instantiated.
  
  /**
   * <p>
   *   Runs sample code.
   * </p>
   * <table border='1'>
   *   <tr><th>Option</th><th>Description</th><th>Mandatory</th></tr>
   *   <tr>
   *     <td>{@code -key <app_key>}</td>
   *     <td>Flickr application key.</td>
   *     <td>Yes.</td>
   *   </tr>
   *   <tr>
   *     <td>{@code -id <photoset_id>}</td>
   *     <td>Id to the photo set.</td>
   *     <td>Yes.</td>
   *   </tr>   
   * </table>
   * @param args Command line arguments, see above table for accepted options.
   */
  public static void main(String[] args) 
  { 
    try
    {
      CommandLineParser clp=new CommandLineParser(args);
      
      String id=clp.mandatoryValue("-id");
      
      // EXAMPLE CODE START
      
      FlickrSession session=new FlickrSession(clp);
      
      Request req=session.createRequest("flickr.photosets.getPhotos");
      req.setArg("photoset_id", id);
      
      FlickrObject responseFo=req.perform();
      
      //
      // Not something you will normally do in a propper application but nice
      // to do when exploring.
      //
      
      // Printing the parsed FlickrObject hierarcy:
      
      System.out.println("---");
      System.out.println("Response hierarchy:");
      responseFo.printHierarchy();
      System.out.println("---");
      
      //
      // More the thing an application would do:
      //
      
      // Generate URLs to the image's big size sources:
      FlickrObject photoSetFo=responseFo.getMandatoryChild("photoset");
      FlickrObject[] photoFos=photoSetFo.getChildren("photo");
      int n=photoFos.length;
      
      String[] srcUrls=new String[n]; // Array to put generated URLs.
      
      // Being sample code we also print URLs to standard out :-)
      System.out.println();
      System.out.println("Source URLs for 'big' size versions of photos:");
      System.out.println();
      for (int i=0; i<n; i++)
      {
        FlickrObject photoFo=photoFos[i];
        String url=FlickrUrls.createPhotoUrlSpec(photoFo, "b");
        
        // Being sample code we also print URLs to standard out :-)
        System.out.println(url); 
        
        srcUrls[i]=url;
      }
      
      // Generate the kind of HTML that is compliant with Flickr guidelines
      // when putting images to sites not Flickr.
      
      // Being sample code we also print HTML to standard out :-)
      System.out.println();
      System.out.println("HTML to include images on web pages external to "+
        "Flickr");
      System.out.println();
      String nsid=photoSetFo.getMandatoryAttribute("owner");
      for (int i=0; i<n; i++)
      {
        FlickrObject photoFo=photoFos[i];
        String photoId=photoFo.getMandatoryAttribute("id");
        String title=photoFo.getAttribute("title");
        String srcUrl=srcUrls[i];
        String code=FlickrUrls.createPhotoHtmlLink(nsid, photoId, srcUrl, 
          title);
        
        // Being sample code we also print HTML to standard out :-)
        System.out.println(code);
        System.out.println();
      }
      
      // EXAMPLE CODE END
      
      System.exit(0);
    }
    catch (IOException iox)
    {
      iox.printStackTrace();
      System.exit(-1);
    }
  }
}
